﻿package mvc_controller
{
	import flash.events.*;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;

	import mvc_model.Model;
	import mvc_model.ProjectsPageModel;
	import mvc_view.View;
	import mvc_model.Downloader;

	public class ProjectsController
	{
		private var _model:ProjectsPageModel;
		private var _view:View;

		public function ProjectsController(model:ProjectsPageModel, view:View)
		{
			_model = model;
			_view = view;
		}

		// next user button
		public function addNextProjectButton(button:DisplayObject)
		{
			if (button == null)
			{
				trace("null button");
			}

			button.addEventListener(MouseEvent.CLICK, nextProject);
			button.addEventListener(MouseEvent.ROLL_OVER, doRollOver);
			button.addEventListener(MouseEvent.ROLL_OUT, doRollOut);

			MovieClip(button).buttonMode = true;
			MovieClip(button).useHandCursor = true;
		}

		public function nextProject(event:Event = null)
		{
			if (_model == null)
			{
				trace("model null");
			}
			var index:int = _model.currentIndex + 1;
			_model.setCurrentIndex(index);
		}

		// previous user button
		public function addPreviousProjectButton(button:DisplayObject)
		{
			if (button == null)
			{
				trace("null button");
			}

			button.addEventListener(MouseEvent.CLICK, previousProject);
			button.addEventListener(MouseEvent.ROLL_OVER, doRollOver);
			button.addEventListener(MouseEvent.ROLL_OUT, doRollOut);

			MovieClip(button).buttonMode = true;
			MovieClip(button).useHandCursor = true;
		}
		
		public function previousProject(event:Event = null)
		{
			_model.setCurrentIndex(_model.currentIndex - 1);
		}

		public function addDownloadButton(button:DisplayObject)
		{
			if (button == null)
			{
				trace("download button null");
			}

			button.addEventListener(MouseEvent.CLICK, downloadProject);
			button.addEventListener(MouseEvent.ROLL_OVER, doRollOver);
			button.addEventListener(MouseEvent.ROLL_OUT, doRollOut);

			MovieClip(button).buttonMode = true;
			MovieClip(button).useHandCursor = true;
		}

		public function downloadProject(event:Event = null)
		{
			trace("download buttton click");
			var downloader:Downloader = new Downloader();
			downloader.Download(_model.url);
		}

		function doRollOver(e:MouseEvent):void
		{
			//trace(e.currentTarget.name);
			switch (e.currentTarget.name)
			{
				case "arrow_right_btn" :
					e.currentTarget.scaleX = 1.5;
					break;
				case "arrow_left_btn" :
					e.currentTarget.scaleX = 1.5;
					break;
				case "download_btn" :
					e.currentTarget.download_btn_txt.textColor = 0x00FF00;
					break;
			}
		}

		function doRollOut(e:MouseEvent):void
		{
			//trace(e.currentTarget.name);
			switch (e.currentTarget.name)
			{
				case "arrow_right_btn" :
					e.currentTarget.scaleX = 1;
					break;
				case "arrow_left_btn" :
					e.currentTarget.scaleX = 1;
					break;
				case "download_btn" :
					e.currentTarget.download_btn_txt.textColor = 0x333333;
					break;
			}
		}
	}
}